package tgc.edu.xzy.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.xzy.custom.AjaxResult;
import tgc.edu.xzy.custom.JpgridUtils;
import tgc.edu.xzy.entity.Invoice;
import tgc.edu.xzy.entity.Journal;
import tgc.edu.xzy.entity.Lessee;
import tgc.edu.xzy.entity.SysUser;
import tgc.edu.xzy.entity.User;
import tgc.edu.xzy.form.InvoiceForm;
import tgc.edu.xzy.security.UserUtils;
import tgc.edu.xzy.service.ElectronicService;
import tgc.edu.xzy.service.LogService;
import tgc.edu.xzy.service.UserService;
import tgc.edu.xzy.service.ZhService;

@Controller
@RequestMapping(value="/invoice")
public class InvoiceController {
	@Autowired
	private ElectronicService electronicService;
	@Autowired
	private LogService logService;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private UserService userService;
	@Autowired
	private ZhService zhService;
	
	@RequestMapping(value="dzfp")
	public String dzfp() {
		return "LesseeAdmin/dzfp";
	}
	
	@RequestMapping(value="all")
	public String all(ModelMap map) {
		SysUser user = userUtils.getUser();
		User user1 = userService.findById(user.getId());
		List<Lessee> list = zhService.findByLesseeAdminId(user1.getLesseeAdmin().getId());
		map.put("lessee", list);
		return "User/invoice_all";	
	}
	
	@RequestMapping(value="list")
	@ResponseBody
	public Object list(InvoiceForm form) {
		Pageable pageable = form.buildPageable();
		Page<Invoice> page = null;
		if(form.getId() == null) {
			page = electronicService.findAll(pageable);			
		}else {
			page = electronicService.findAll(form.buildSpes(), pageable);
		}
		HashMap<String, Object> result = JpgridUtils.getPageResult(page);
		return result;
	}
	
	@RequestMapping(value="/list1")
	@ResponseBody
	public Object list1(InvoiceForm form) {
		SysUser user = userUtils.getUser();
		Pageable pageable = form.buildPageable();
		Page<Invoice> page = null;
		if(form.getId() == null) {
			page = electronicService.findByLesseeLesseeAdminId(user.getId(), pageable);			
		}else {
			page = electronicService.findByLesseeLesseeAdminId(user.getId(), form.buildSpes(), pageable);
		}
		HashMap<String, Object> result = JpgridUtils.getPageResult(page);
		return result;
	}
	
	@RequestMapping(value="save")
	@ResponseBody
	public Object save(Invoice model) {
		Date d = new Date();  
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		String dateNowStr = sdf.format(d);
		if(model.getNumber() == null || model.getNumber() == "") {
			Calendar now = Calendar.getInstance();
			String year = String.valueOf(now.get(Calendar.YEAR));  
			String month = String.valueOf((now.get(Calendar.MONTH) + 1)); 
			String day = String.valueOf(now.get(Calendar.DAY_OF_MONTH));
			String intFlag = String.valueOf((int)(Math.random() * 10000 + 1));
	        model.setDate(dateNowStr);
	        model.setNumber(year+month+day+intFlag);
	        model.setCode(intFlag+month+day+intFlag);
	        model.setState("未报销");
	        electronicService.save(model);
	        
	        SysUser user = userUtils.getUser();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			String data = df.format(new Date());
			Journal journal = new Journal();
			journal.setDate(data);
			journal.setUsername(user.getUsername());
			journal.setOperationName("添加发票");
			logService.save(journal);
	        return "all";
		}else {
			model.setEndDate(dateNowStr);
			model.setState("已报销");
			electronicService.save(model);
			return AjaxResult.build(true, "OK");
		}
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public void delete(String ids) {
		String[] id = ids.split(",");   //拆分字符串，
		for(int i = 0; i < id.length; i++) {
			electronicService.deleteById(Integer.parseInt(id[i]));
		}
	}
}
